<?php
namespace app\manage\controller;

use think\Controller;
use think\Request;
use think\Validate;
use think\Db;
use Config;

/**合同管理
 * Class UnitBuilding
 * @package app\manage\controller
 */
class ContracTmanagement extends Controller{

    /**
     * 合同显示
     * @return \think\response\Json
     */
    public function TenantList(){
        $model=new \app\manage\model\ContracTmanagement();
        $date=$model->index();
        returnJson(200,"成功",$date)->send();die;
    }

    /**
     * 选择住房还是商铺
     *  @return \think\response\Json
     */
    public function TmanagementType(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $date=$model->h_type($input);
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 手机号添加租客
     *  @return \think\response\Json
     */
    public function TmanagementPhone(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        if(!isset($input["phone"])){
            returnJson(-410,"手机号")->send();die;
        }
        $validate = new Validate([
            ['phone', 'require|number', '请填写手机号|手机号必须是数字']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }
     //   $h_id = trim( $input['phone'] );
        $date=$model->userPhone($input);
        if($date==null){
            returnJson(-412,"手机号不存在")->send();die;
        }else{
            returnJson(200,"成功",$date)->send();die;
        }

    }
    /**
     * 合同添加
     * @return \think\response\Json
     */
    public function TmanagementAdd(){
        $model=new \app\manage\model\ContracTmanagement();
        //get的时候显示后台设置的栋数和单元楼post的时候添加
        if(Request::instance()->isGet()){
            $data=$model->type();
            //判断后台有没有设置单元楼栋数
            if($data==2){
                returnJson(-411,"失败")->send();die;
            }else{
                returnJson(200,"成功",$data)->send();die;
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['c_number', 'require', '请填写合同编号'],
                ['c_a_time', 'require', '请选择开始时间'],
                ['c_d_time', 'require', '请选择结束时间'],
                ['c_rent', 'require', '实际租金不能为空'],
                ['c_deposit', 'require', '实际押金不能为空']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }

            $date=$model->TmanagementAdd($data);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-412,"添加失败")->send();die;
            }
        }
    }
    /**
     * 合同修改
     * @return \think\response\Json
     */
    public function TmanagementUpdate(){
        $model=new \app\manage\model\ContracTmanagement();
        //get的时候显示后台设置的栋数和单元楼post的时候添加
        if(Request::instance()->isGet()){
            $data = input();
            $h_id = trim( $data['t_id'] );
            $data=$model->TmanagementOne($h_id);
            //判断后台有没有设置单元楼栋数
            if($data==2){
                returnJson(-411,"合同不存在")->send();die;
            }else{
                returnJson(200,"成功",$data)->send();die;
            }
        }else{
            $data = input();
            $validate = new Validate([
                ['c_number', 'require', '请填写合同编号'],
                ['c_a_time', 'require', '请选择开始时间'],
                ['c_d_time', 'require', '请选择结束时间'],
                ['c_rent', 'require', '实际租金不能为空'],
                ['c_deposit', 'require', '实际押金不能为空']
            ]);
            if(!$validate->check($data))
            {
                returnJson(-1001,  $validate->getError())->send();die;
            }

            $date=$model->TmanagementUpload($data);
            if($date){
                returnJson(200,"成功")->send();die;
            }else{
                returnJson(-412,"添加失败")->send();die;
            }
        }
    }

    /**
     * 编辑的时候删除合租人
     *  @return \think\response\Json
     */
    public function tenantDel(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $h_id = trim( $input['t_c_id'] );
        $date=$model->tenantDel($h_id);
        if($date==null){
            returnJson(-412,"删除失败")->send();die;
        }else{
            returnJson(200,"成功")->send();die;
        }
    }
    /**
     * 编辑的时候添加合租人
     *  @return \think\response\Json
     */
    public function tenantAdd(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        if(!isset($input["phone"])){
            returnJson(-410,"手机号未填写")->send();die;
        }
        $validate = new Validate([
            ['phone', 'require|number', '请填写手机号|手机号必须是数字']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }
        $date=$model->tenantAdd($input);
        if($date==2){
            returnJson(200,"不能添加重复数据")->send();die;
        }elseif($date==3) {
            returnJson(-436,"没有租客信息")->send();die;
        } else{
            returnJson(200,"成功")->send();die;
        }
    }
    /**
     * 编辑的时候删除房源
     *  @return \think\response\Json
     */
    public function housingDel(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $h_id = trim( $input['c_h_id'] );
        $date=$model->housingDel($h_id);
        if($date==null){
            returnJson(-412,"删除失败")->send();die;
        }else{
            returnJson(200,"成功")->send();die;
        }
    }
    /**
     * 编辑的时候添加房源
     *  @return \think\response\Json
     */
    public function housingAdd(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $date=$model->housingAdd($input);
        if($date==2){
            returnJson(-412,"该房源已被绑定")->send();die;
        }
        if($date==null){
            returnJson(-412,"添加失败")->send();die;
        }else{
            returnJson(200,"成功",$date)->send();die;
        }
    }
    /**
     * 根据编号找到数据
     *  @return \think\response\Json
     */
    public function h_housing(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $date=$model->h_housing($input);
        if($date==null){
            returnJson(-412,"失败")->send();die;
        }else{
            returnJson(200,"成功",$date)->send();die;
        }
    }
    /**
     * 合同查看
     * @return \think\response\Json
     */
    public function TenantCount(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $h_id = trim( $input['t_id'] );
        $date=$model->T_One($h_id);
        returnJson(200,"成功",$date)->send();die;
    }
    /**
     * 合同查看删除缴费记录
     * @return \think\response\Json
     */
    public function payDel(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $h_id = trim( $input['pay_id'] );
        $date=$model->payDel($h_id);
        if($date){
            returnJson(200,"成功")->send();die;
        }else{
            returnJson(-410,"失败")->send();die;
        }
    }
    /**
     * 合同查看添加缴费记录
     * @return \think\response\Json
     */
    public function payAdd(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $validate = new Validate([
            ['pay_time', 'require', '缴费时间不能为空']
        ]);
        if(!$validate->check($input))
        {
            returnJson(-1001,  $validate->getError())->send();die;
        }
        $date=$model->payAdd($input);
        if($date){
            returnJson(200,"成功",$date)->send();die;
        }else{
            returnJson(-410,"失败")->send();die;
        }
    }
    /**
     * 根据合同号找到对用的用户
     * @return \think\response\Json
     */
    public function userList(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $h_id = trim( $input['c_id'] );
        $date=$model->userList($h_id);
      //  var_dump($date);exit;
        if($date){
            returnJson(200,"成功",$date)->send();die;
        }else{
            returnJson(-410,"失败")->send();die;
        }
    }
    /**
     * 根据合同号找到对用的住房
     * @return \think\response\Json
     */
    public function housingList(){
        $model=new \app\manage\model\ContracTmanagement();
        $input = input();
        $h_id = trim( $input['c_id'] );
        $date=$model->housingList($h_id);
        //  var_dump($date);exit;
        if($date){
            returnJson(200,"成功",$date)->send();die;
        }else{
            returnJson(-410,"失败")->send();die;
        }
    }

    /**
     * 合同状态改变
     * @return \think\response\Json
     */
    public function contractType(){
        $model=new \app\manage\model\ContracTmanagement();
        $date=$model->contractType();
          var_dump($date);exit;

    }

}